#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from utils import utils_data
from check_test_json_format import check_test_json
from check_testcase import check_testcase
from check_testsuite import check_testsuite
from check_signature import check_signature
from check_hapname import check_hapname
from check_testjson_config import JsonConfigChecker
from check_test_filename import check_test_filename
from check_testcomment import TCChecker
from check_hvigor import check_hvigor
from check_assertion import check_assertion
from check_syscap import check_syscap
from check_partname import check_partname
from check_level import Level
import os
import logging
import json

logger = logging.getLogger('my_logger')

logger.debug("日志输出路径：%s", utils_data.log_dir)
logger.debug("执行的规则列表：%s", utils_data.rules)
logger.debug("报告输出路径：%s", utils_data.output_path)
logger.debug("文件修改参数：%s", utils_data.params_file_path)
logger.debug("项目基础路径：%s", utils_data.project_dir)

result_file_path = os.path.join(utils_data.output_path)
if os.path.exists(result_file_path):
    os.remove(result_file_path)

action_map = {
    "XTS.CHECK.TESTJSON.01": check_test_json,
    "XTS.CHECK.TESTCASE.01": check_testcase,
    "XTS.CHECK.TESTSUITE.01": check_testsuite,
    "XTS.CHECK.SETENFORCE.01": JsonConfigChecker.check_setenforce_zero,
    "XTS.CHECK.SIGNATURE.01": check_signature,
    "XTS.CHECK.HAPNAME.01": check_hapname,
    "XTS.CHECK.APPFREEEZE.01": JsonConfigChecker.check_appfreeze_filter,
    "XTS.CHECK.TESTFILENAME.01": check_test_filename,
    "XTS.CHECK.TCNUMBER.01": TCChecker.check_tc_number,
    "XTS.CHECK.TCNAME.01": TCChecker.check_tc_name,
    "XTS.CHECK.TCDESC.01": TCChecker.check_tc_desc,
    # "XTS.CHECK.TCLEVEL.01": TCChecker.check_tc_level,
    # "XTS.CHECK.TCSIZE.01": TCChecker.check_tc_size,
    # "XTS.CHECK.TCTYPE.01": TCChecker.check_tc_type,
    "XTS.CHECK.HVIGOR.01": check_hvigor,
    "XTS.CHECK.ASSERTION.01": check_assertion,
    "XTS.CHECK.SYSCAP.01": check_syscap,
    "XTS.CHECK.PARTNAME.01": check_partname,
    "XTS.CHECK.ABILITYMS.01": JsonConfigChecker.check_abilityms_false,
    "XTS.CHECK.LEVEL.01": Level.check_level
}

if "XTS.ALL.RULE" in utils_data.rules:
    for func in action_map.values():
        func()
else:
    for rule in utils_data.rules:
        if rule in action_map:
            action_map[rule]()
        else:
            logger.info(f"指定了未知的规则: {rule}")


def ensure_result_file_exists():
    result_file_path = os.path.join(utils_data.output_path)
    if not os.path.exists(result_file_path):
        try:
            with open(result_file_path, 'w', encoding='utf-8') as f:
                json.dump([], f, ensure_ascii=False, indent=4)
        except Exception as e:
            logger.error(f"创建或写入 xts_result.json 文件时出错: {e}")


ensure_result_file_exists()
